﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bouyei.Discoverer
{
    using Class;

    public partial class Configuration : Form
    {
        BaseClass baseClass = new BaseClass();

        private void InitDefaultChecked()
        {
            ckblistLinklayer.SetItemChecked(0, true);
            ckblistPrototollayer.SetItemChecked(0, true);
            ckblistPrototollayer.SetItemChecked(1, true);
        }

        public Configuration()
        {
            InitializeComponent();
            try
            {
                for (int i = 0; i < GlobalVariable.CaptureAdapterList.Count; ++i)
                {
                    cmbAdapterList.Items.Add(GlobalVariable.CaptureAdapterList[i].CaptureDisplayName);
                }
            }
            catch
            { }
            if (GlobalVariable.CurrentCaptureAdapter != null)
            {
                if(GlobalVariable.CurrentCaptureAdapter.CaptureId<cmbAdapterList.Items.Count)
                {
                    cmbAdapterList.SelectedIndex = GlobalVariable.CurrentCaptureAdapter.CaptureId;
                }
            }
            else
            {
                if (cmbAdapterList.Items.Count > 0)
                {
                    cmbAdapterList.SelectedIndex = 0;
                    GlobalVariable.CurrentCaptureAdapter = GlobalVariable.CaptureAdapterList[0];
                }
            }

            InitDefaultChecked();
        }

        private void btnDetail_Click(object sender, EventArgs e)
        {
            if (GlobalVariable.CurrentCaptureAdapter == null) return;

            if (!GlobalVariable.CurrentCaptureAdapter.CaptureDevice.Started)
            {
                //GlobalVariable.CurrentCaptureAdapter.Open();

                //string content = string.Format("描述:{0};\n过滤规则:{1};\n链路类型:{2};\nMAC地址:{3};\n丢包数:{4};\n接口丢包数:{5};\n捕获包:{6}",
                //    GlobalVariable.CurrentCaptureAdapter.Description,
                //    GlobalVariable.CurrentCaptureAdapter.Filter,
                //    GlobalVariable.CurrentCaptureAdapter.LinkType,
                //    baseClass.ToMacAddressFormat(GlobalVariable.CurrentCaptureAdapter.MacAddress.GetAddressBytes()),
                //    GlobalVariable.CurrentCaptureAdapter.Statistics.DroppedPackets,
                //    GlobalVariable.CurrentCaptureAdapter.Statistics.InterfaceDroppedPackets,
                //    GlobalVariable.CurrentCaptureAdapter.Statistics.ReceivedPackets);

                AdapterInfo frmAdapterInfo = new AdapterInfo(GlobalVariable.CurrentCaptureAdapter.ToString());
                frmAdapterInfo.Show();
            }
        }

        private void cmbAdapterList_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cmbAdapterList.SelectedIndex >= 0)
                GlobalVariable.CurrentCaptureAdapter = GlobalVariable.CaptureAdapterList[cmbAdapterList.SelectedIndex];
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCertain_Click(object sender, EventArgs e)
        {
            GlobalVariable.CaptureFilter = "src port 80 or dst port 80 or src port 443 or dst port 443";
            GlobalVariable.networkCapture.SetCaptureFilter(GlobalVariable.CaptureFilter);
            this.Close();
        }

        private void ckblistLinklayer_SelectedValueChanged(object sender, EventArgs e)
        {
            int cindex = ckblistLinklayer.SelectedIndex;
            if (cindex < 0) return;

            bool cchecked = ckblistLinklayer.GetItemChecked(cindex);

            for (int i = 0; i < ckblistLinklayer.Items.Count; ++i)
            {
                if (i != cindex && ckblistLinklayer.GetItemChecked(i) == cchecked)
                {
                    ckblistLinklayer.SetItemChecked(i, !cchecked);
                }
            }

            if (ckblistLinklayer.GetItemChecked(0))
            {
                ckblistPrototollayer.Enabled = true;
            }
            else
            {
                ckblistPrototollayer.Enabled = false;
            }
        }

        private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (!ckblistLinklayer.GetItemChecked(0) && e.TabPageIndex==1)
            {
                e.Cancel = true;
            }
        }
    }
}
